﻿using UnityEngine;
using System.Collections;
using DICE_GS;

public class AddMaterialUnit : MonoBehaviour {
	public UILabel AddUnitText;
	public UILabel warningText;
	public GameObject myUnitPrefab;
	
	// Use this for initialization
	void Start () {
		if( MyInfo.Instance.LastSelectedUnitIDs.Count != 0 || MyInfo.Instance.LastSelectedUnitDBIDs.Count != 0 )
		{
			AddUnitText.gameObject.SetActive(false);
			warningText.gameObject.SetActive(false);
			
			for(int i=0; i<MyInfo.Instance.LastSelectedUnitIDs.Count; i++)
			{
				GameObject obj = LTPrefabManager.InstantiatePrefab(PrefabTypes.MaterialUnitCard_Temp, Vector3.zero, Quaternion.identity) as GameObject;
//				GameObject obj = Instantiate(myUnitPrefab, new Vector3(0.5f, 0.5f, -1f), Quaternion.identity) as GameObject;
	
				obj.transform.parent = this.transform;
				obj.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
	
				UnitInfo SelectedUnit = obj.GetComponent<SelectUnitButton>().info;
	//				SelectedUnit.UnitClassID = (i % 4) + 1;
				SelectedUnit.UnitClassID = (int)MyInfo.Instance.LastSelectedUnitIDs[i];
				SelectedUnit.UnitDBID = (int)MyInfo.Instance.LastSelectedUnitDBIDs[i];
	
//				UISprite[] sprites = obj.GetComponentsInChildren<UISprite>();
				CUnitData unitData = CUnitDataManager.Instance.GetUnitData(SelectedUnit.UnitClassID);
				Texture2D tex = Resources.Load(unitData.name) as Texture2D;
				if( tex != null )
					obj.GetComponent<SelectUnitButton>().unitImage.mainTexture = tex;
				else
					obj.GetComponent<SelectUnitButton>().unitImage.mainTexture = Resources.Load("N_ea_01") as Texture2D;
				obj.GetComponent<SelectUnitButton>().unitName.gameObject.SetActive(false);
				obj.GetComponent<SelectUnitButton>().unitLevel.gameObject.SetActive(false);
				obj.GetComponent<SelectUnitButton>().unitGrade.gameObject.SetActive(false);
				obj.GetComponent<SelectUnitButton>().starImage.gameObject.SetActive(false);

				obj.collider.enabled = false;
	
//				foreach (UISprite sprite in sprites)
//				{
//					if (sprite.spriteName.IndexOf("frame") == -1)
//					{
//						sprite.spriteName = unitData.name;
//					}
//					else
//					{
//						sprite.spriteName = "select_frame";
//					}
//				}		
			}
			
			GetComponent<UIGrid>().Reposition();
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
